Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hook-store): override Hook Gas limit and tokens balances with Tenderly Simulation Data #5039

Conversation

yvesfracari
Copy link
Contributor

@yvesfracari yvesfracari commented Oct 24, 2024

Summary

Using gasUsed data of Tenderly simulation (added on bff on this PR) to override gasLimit hook data.

To Test

  1. Run the bff server locally in this PR context
  2. Run the app locally connecting it with your local bff
  3. Use the Build Hook dApp to create one simple hook with a low gas limit. The data that I used to test was a pre-hook with:
  • Target: 0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14
  • Gas Limit: 10
  • Calldata: 0x
  1. Select two tokens go until the swap review and check the gas limit. Check if the gas limit is different from the one that you inputted and 10% bigger than the simulated one (click on the Tenderly simulation link to check the simulated)
image

Note: The PR is in draft until the balanceDiff PR is merged. But feel free to test it (only the diffs will have that context as well)

Copy link

vercel bot commented Oct 24, 2024

@yvesfracari is attempting to deploy a commit to the cow Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@elena-zh elena-zh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All is good rn besides the issue (edge-case) we decided to address later.
Thanks!

Copy link
Contributor

@anxolin anxolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description of this PR made me think the scope is smaller. I don't think the description and code changes maches. Most of this PR is about calculating the aggregated balance given the current user's balance and the result from the similation.

const { preHooks, postHooks } = useHooks()
const preHookBalanceDiff = usePreHookBalanceDiff()

const orderMockBalanceDiff = useMemo(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why mock?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the lack of documentation. This is used mainly when the swap isn't simulated (like if the user has only preHooks). I changed a bit the variable name and comments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks

return bigA.add(bigB).toString()
}

function mergeBalanceDiffs(first: BalancesDiff, second: BalancesDiff): BalancesDiff {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the type of the BalancesDiff I see is a structure to hold token balances. Having Diff in its name feels a bit constraining cause you can use it for example to hold the current balances, etc.

I would drop the diff from the name and would make the merging utilities more generic so they live not in the hookStore but in a more central place. Or at least can be moved to a utility instead of being in a hook file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main difference between this and a token balances is that it can be negative. Another difference between this and the token balances used on the rest of the app is the first key. Another difference is that the outer key of BalanceDiff is the address which the difference is related to while the BalanceState of the rest of the app the outer key is the chainId since it is always related to the connected account. For now, I don't see any other place on the app where we would store balances diff from addresses, so I think that there isn't too much gain of moving this to a utility for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, makes sense

result[address][token] = addBigNumberStrings(result[address][token], second[address][token])
} catch (error) {
console.error(`Error adding balances for address ${address} and token ${token}:`, error)
throw error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this logic very similar to the other one that would apply the diffs to the current balances?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is similar, however is used for only one value instead of all of them and that is why I am not importing the function from there.

@anxolin
Copy link
Contributor

anxolin commented Nov 7, 2024

Gas price is overriden as described.

In this case, simulation shows 21K gas:
image

I can see it replaces the 10 with the 21K+10%
image

Are we sure we want to add the buffer? Did we encounter issues with execution failing due to gas?

@yvesfracari yvesfracari changed the title feat(hook-store): override Hook Gas limit with Tenderly Simulation Data feat(hook-store): override Hook Gas limit and tokens balances with Tenderly Simulation Data Nov 7, 2024
@yvesfracari
Copy link
Contributor Author

Thanks for the review @anxolin ! I tried to resolve / reply all your questions. About the PR size, sorry it was supposed to be two separated PRs but then it was requested to be merged and I forget to update the description.

About the gas limit, I already faced some issues of hooks not being executed due to out of gas, but never when the gas was defined by the tenderly. About the 10%, I just keep an old logic that existed on other hooks.

@yvesfracari
Copy link
Contributor Author

As discussed on the channel, I removed the buffer of the Tenderly simulation gas.

Copy link
Contributor

github-actions bot commented Nov 7, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@yvesfracari yvesfracari force-pushed the pedro/cow-414-make-hook-gas-limit-override-it-with-tenderly-simulation branch from d2b652d to 1745ea6 Compare November 8, 2024 15:04
@yvesfracari
Copy link
Contributor Author

1 out of 2 committers have signed the CLA.✅ @yvesfracari@pedro YvesPedro Yves seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.You can retrigger this bot by commenting recheck in this Pull Request

This was due some commits with a wrong author, already fixed it. Please ignore it 😅

@elena-zh
Copy link
Contributor

Changes LGTM.
Not for the current PR, but for the future updates, it would be nice to have one generic 're-run simulation' button that will retrigger bundle on click instead of having several ones with the same action :) Something link this:
image

@fairlighteth, WDYT? :)

@vercel vercel bot temporarily deployed to Preview – widget-configurator November 11, 2024 10:05 Inactive
@vercel vercel bot temporarily deployed to Preview – cosmos November 11, 2024 10:07 Inactive
@vercel vercel bot temporarily deployed to Preview – cowfi November 11, 2024 10:10 Inactive
@vercel vercel bot temporarily deployed to Preview – explorer-dev November 11, 2024 10:13 Inactive
@anxolin
Copy link
Contributor

anxolin commented Nov 11, 2024

@yvesfracari I think you have some lint issues:

image

@vercel vercel bot temporarily deployed to Preview – swap-dev November 11, 2024 10:18 Inactive
@anxolin
Copy link
Contributor

anxolin commented Nov 11, 2024

I want to merge this PR so we start to consolidate things. Any enhancement we can try to do iterativelly to not keep this one open for too long

For this, I created this PR which applies the changes of this PR plus fixes the lint issues I posted above #5080

@anxolin anxolin closed this Nov 11, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Nov 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants